package cn.wolfcode.luowowo.website.controller;


import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyCommend;
import cn.wolfcode.luowowo.article.domain.StrategyContent;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.service.IStrategyEsService;
import cn.wolfcode.luowowo.search.vo.StatisVO;
import cn.wolfcode.luowowo.website.annotion.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;

import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("strategy")
public class StratrgyController {

    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyCommentService strategyCommentService;

    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyTagService strategyTagService;

    @Reference
    private IStrategyCommendService strategyCommendService;

    @Reference
    private IStrategyStatisVOService strategyStatisVOService;

    @Reference
    private IStrategyEsService strategyTemplateSearchService;


    @RequestMapping("")
    public String index (Model model){
        List<StrategyCommend> top5 = strategyCommendService.getTop5();
        model.addAttribute("commends",top5);
     List<StrategyStatisVO> hotCds=  strategyStatisVOService.queryHotSotrTop10();
        model.addAttribute("hotCds",hotCds);
  List<StrategyStatisVO> abroadCds =       strategyStatisVOService.queryAbroadSortTop10();
        model.addAttribute("abroadCds",abroadCds);
  List<StrategyStatisVO> unabroadCds =       strategyStatisVOService.queryChinaSortTop10();
        model.addAttribute("unabroadCds",unabroadCds);

        //主题推荐
       List<Map<String ,Object> > data= strategyTemplateSearchService.queryThemeCommend();
        model.addAttribute("themeCds",data);

        //国内
    List<StatisVO> chinas=   strategyTemplateSearchService.queryConditionChina();
        model.addAttribute("chinas",chinas);
        //国外
        List<StatisVO> aborads=   strategyTemplateSearchService.queryConditionAborads();
        model.addAttribute("abroads",aborads);
        //主题
        List<StatisVO> thems=   strategyTemplateSearchService.queryConditionthems();

        model.addAttribute("themes",thems);
        return  "strategy/index";
    }







    @RequestMapping("/detail")
    public String detail(Model model, Long id ){
        StrategyDetail detail = strategyDetailService.get(id);
        StrategyContent conent = strategyDetailService.getConent(id);
        detail.setStrategyContent(conent);
        model.addAttribute("detail",detail);

        strategyStatisVOService.increaseViewNum(id,1);

      StrategyStatisVO vo = strategyStatisVOService.getStrategyStraisVo(id);
        model.addAttribute("vo",vo);

        //排行加分
        strategyStatisVOService.addscoreHot(id,1);


        return "strategy/detail";
    }

    @RequestMapping("list")
    public  String list (Model model, @ModelAttribute("qo")StrategyDetailQuery qo){
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);

        model.addAttribute("dest",dest);
        model.addAttribute("tags",strategyTagService.list());

        model.addAttribute("pageInfo",strategyDetailService.query(qo));

        return "strategy/list";
    }
@RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(StrategyComment comment, @UserParam UserInfo userInfo){
        comment.setCity(userInfo.getCity());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setLevel(userInfo.getLevel());
        comment.setUserId(userInfo.getId());
        comment.setUsername(userInfo.getNickname());
        comment.setCreateTime(new Date());
        strategyCommentService.save(comment);

        //评论数增加
    strategyStatisVOService.increassReplyNum(comment.getDetailId(),1);
    StrategyStatisVO vo = strategyStatisVOService.getStrategyStraisVo(comment.getDetailId());

    strategyStatisVOService.addscoreHot(comment.getDetailId(),1);
    return AjaxResult.SUCCESS.addData(vo.getReplynum());
}
@RequestMapping("/comment")
public  String comment(Model model, @ModelAttribute("qo") StrategyCommentQuery qo){


       Page page =  strategyCommentService.query(qo);
    model.addAttribute("page",page);


return "strategy/commentTpl";
}


@RequestMapping("/commentThumbUp")
@ResponseBody
    public Object commentThumvUp(String toid,@UserParam UserInfo userInfo){

        strategyCommentService.commentThumbUp(toid,userInfo.getId());
        return AjaxResult.SUCCESS;
}
//收藏
@RequestMapping("/favor")
@ResponseBody
    public  Object favor(Long sid ,@UserParam UserInfo userInfo){
        if(userInfo == null){
            AjaxResult result = new AjaxResult(false, "请登录");
            result.setCode(102); //未登录
            return result;
        }

    boolean ret = strategyStatisVOService.favor(sid, userInfo.getId());
    StrategyStatisVO vo = strategyStatisVOService.getStrategyStraisVo(sid);

        //收藏
        strategyStatisVOService.addScoreAbroAndChina(sid,ret?1:-1);


    return new AjaxResult(ret,"").addData(vo);
}


    @RequestMapping("/strategyThumbup")
    @ResponseBody
    public  Object strategyThumbup(Long sid ,@UserParam UserInfo userInfo){
        if(userInfo == null){
            AjaxResult result = new AjaxResult(false, "请登录");
            result.setCode(102); //未登录
            return result;
        }
        //顶
        // 今天顶过了
        boolean ret = strategyStatisVOService.strategyThumbup(sid, userInfo.getId());
        StrategyStatisVO vo = strategyStatisVOService.getStrategyStraisVo(sid);
        //点赞
        if(ret) {
            strategyStatisVOService.addScoreAbroAndChina(sid, 1);
        }
        return new AjaxResult(ret,"").addData(vo);
    }




}
